﻿package com.tudou.home.model.ad
{
    import com.tudou.consts.*;
    import com.tudou.cookie.*;
    import com.tudou.home.commands.ad.*;
    import com.tudou.log.*;
    import com.tudou.track.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import org.puremvc.as3.patterns.proxy.*;

    public class AdListProxy extends Proxy
    {
        private var aop:AOPTracker;
        private var tudouSo:ICookie;
        private var _currentCacheData:Object;
        private var _loader:Loader;
        public static const NAME:String = "AdListProxy";

        public function AdListProxy()
        {
            super(NAME);
            initObject();
            initListener();
            return;
        }// end function

        public function startCache() : void
        {
            var _loc_1:* = getNoCacheItem();
            if (_loc_1 == null)
            {
                return;
            }
            _currentCacheData = _loc_1;
            if (!_currentCacheData.nLoaded)
            {
                aop.addNodeLog(NodeLog.ADCACHE_REQUEST_START);
                _loader.load(new URLRequest(_currentCacheData.nURL));
            }
            return;
        }// end function

        private function addListItem(param1:XML) : void
        {
            var itemData:Object;
            var itemXML:* = param1;
            var listData:* = tudouSo.getValue(CookieConsts.AD_LIST_DATA);
            var countId:* = uint(tudouSo.getValue(CookieConsts.AD_LIST_COUNT));
            var tempDate:* = new Date();
            var nowTime:* = new Date(tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate()).time;
            if (listData == null)
            {
                listData = new Object();
            }
            try
            {
                if (itemXML.@type == 3 || itemXML.@type == 1)
                {
                    itemData = new Object();
                    itemData.countId = countId;
                    itemData.id = itemXML.@id.toString();
                    itemData.time = nowTime;
                    itemData.nURL = itemXML.text().toString();
                    itemData.nLoaded = false;
                    itemData.iid = itemXML.@iid.toString();
                    itemData.vURL = "";
                    itemData.vLoaded = false;
                    if (listData[itemData.id] == null)
                    {
                        listData[itemData.id] = itemData;
                    }
                }
                else
                {
                    itemData = new Object();
                    itemData.countId = countId;
                    itemData.id = itemXML.@id.toString();
                    itemData.time = nowTime;
                    itemData.nURL = itemXML.text().toString();
                    itemData.nLoaded = false;
                    if (listData[itemData.id] == null)
                    {
                        listData[itemData.id] = itemData;
                    }
                }
            }
            catch (error:Error)
            {
            }
            countId = (countId + 1);
            tudouSo.lock();
            tudouSo.setValue(CookieConsts.AD_LIST_DATA, listData);
            tudouSo.setValue(CookieConsts.AD_LIST_COUNT, countId);
            tudouSo.unlock();
            return;
        }// end function

        private function initListener() : void
        {
            _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, normalCompleteHandler);
            _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, normalIoErrorHandler);
            return;
        }// end function

        public function updateList(param1:XMLList) : void
        {
            var _loc_3:String = null;
            var _loc_4:Object = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:XML = null;
            var _loc_2:* = tudouSo.getValue(CookieConsts.AD_LIST_DATA);
            var _loc_5:* = new Date();
            new Date().date = new Date().date - 2;
            var _loc_6:* = new Date(_loc_5.getFullYear(), _loc_5.getMonth(), _loc_5.getDate()).time;
            for (_loc_3 in _loc_2)
            {
                
                _loc_4 = _loc_2[_loc_3];
                if (_loc_6 > uint(_loc_4.time))
                {
                    delete _loc_2[_loc_3];
                }
            }
            tudouSo.setValue(CookieConsts.AD_LIST_DATA, _loc_2);
            _loc_7 = param1.length();
            _loc_8 = 0;
            while (_loc_8 < _loc_7)
            {
                
                _loc_9 = param1[_loc_8];
                addListItem(_loc_9);
                _loc_8++;
            }
            return;
        }// end function

        private function updateCache() : void
        {
            var _loc_1:* = tudouSo.getValue(CookieConsts.AD_LIST_DATA);
            _loc_1[_currentCacheData.id] = _currentCacheData;
            tudouSo.setValue(CookieConsts.AD_LIST_DATA, _loc_1);
            return;
        }// end function

        private function removeCache(param1:String) : void
        {
            var _loc_2:* = tudouSo.getValue(CookieConsts.AD_LIST_DATA);
            delete _loc_2[param1];
            tudouSo.setValue(CookieConsts.AD_LIST_DATA, _loc_2);
            return;
        }// end function

        private function initObject() : void
        {
            _loader = new Loader();
            tudouSo = TudouSO.getInstance();
            aop = AOPTracker.getInst();
            return;
        }// end function

        private function normalCompleteHandler(event:Event) : void
        {
            TudouLogger.debug(this, "预加载 Loaded:", _currentCacheData.nURL);
            aop.addNodeLog(NodeLog.ADCACHE_REQUEST_COMPLETE);
            _currentCacheData.nLoaded = true;
            updateCache();
            stopLoad();
            sendNotification(AdCommandSet.ADLIST_CACHED);
            return;
        }// end function

        private function normalIoErrorHandler(event:IOErrorEvent) : void
        {
            aop.addNodeLog(NodeLog.ADCACHE_REQUEST_ERROR);
            removeCache(_currentCacheData.id);
            stopLoad();
            sendNotification(AdCommandSet.ADLIST_CACHED);
            return;
        }// end function

        private function stopLoad() : void
        {
            _currentCacheData = null;
            try
            {
                _loader.close();
            }
            catch (error:Error)
            {
            }
            return;
        }// end function

        private function getNoCacheItem() : Object
        {
            var _loc_2:Object = null;
            var _loc_3:Object = null;
            var _loc_1:* = tudouSo.getValue(CookieConsts.AD_LIST_DATA);
            for each (_loc_3 in _loc_1)
            {
                
                if (!_loc_3.nLoaded)
                {
                    if (_loc_2 == null || uint(_loc_2.countId) > uint(_loc_3.countId))
                    {
                        _loc_2 = _loc_3;
                    }
                }
            }
            return _loc_2;
        }// end function

        public function get caching() : Boolean
        {
            return _currentCacheData != null;
        }// end function

    }
}
